package Interfaz;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import LogicaDeNegocio.GestionDeUsuarios;
import net.miginfocom.swing.MigLayout;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dimension;

public class Menu extends JFrame {
	
	private static Menu menu = new Menu();

	private JPanel contentPane;
	private JPanel panelCentral;
	private JPanel panelBotones;
	private JButton botonAltaTitular;
	private JButton botonAltaUsuario;
	private JButton botonEmitirLicencia;
	private JButton botonImprimirLicencia;
	private JButton botonLogout;
	
	private GestionDeUsuarios admUsuario = GestionDeUsuarios.getInstance();


	/**
	 * Create the frame.
	 */
	
	static public Menu getInstance() {

		if (menu == null) {
			menu = new Menu();
		}
		else{
			menu.setVisible(true);
		}
		return menu;
	}
	
	private Menu() {
		setVisible(true);
		this.setMinimumSize(new Dimension(600,500));;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 600, 500);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout());
		this.setContentPane(contentPane);
		
		panelBotones = new JPanel();
		panelBotones.setBorder(new TitledBorder(new LineBorder(new Color(184, 207, 229)), "Opciones", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		contentPane.add(panelBotones,BorderLayout.SOUTH);
		panelBotones.setLayout(new MigLayout("", "fill,grow", ""));
		
		botonAltaTitular = new JButton("Nuevo Titular");
		botonAltaUsuario = new JButton("Nuevo Usuario");
		botonEmitirLicencia = new JButton("Emitir Licencia");
		botonImprimirLicencia = new JButton("Imprimir Licencia");
		botonLogout = new JButton("Salir");
		
		panelBotones.add(botonAltaUsuario);
		panelBotones.add(botonAltaTitular);
		panelBotones.add(botonEmitirLicencia);
		panelBotones.add(botonImprimirLicencia);
		panelBotones.add(botonLogout);
		
		panelCentral = new JPanel();
		panelCentral.setBorder(new TitledBorder(new LineBorder(new Color(184, 207, 229)), "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		contentPane.add(panelCentral,BorderLayout.CENTER);
		
		botonAltaTitular.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				contentPane.remove(panelCentral);
				panelCentral = new AltaTitular();
				contentPane.add(panelCentral,BorderLayout.CENTER);
				setSize(new Dimension(601,500));
			}
		});
		
		botonAltaUsuario.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				contentPane.remove(panelCentral);
				panelCentral = new AltaUsuario();
				contentPane.add(panelCentral,BorderLayout.CENTER);
				setSize(new Dimension(600,501));
			}
		});
		
		botonLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				new Login();
			}
		});
		
		botonEmitirLicencia.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				contentPane.remove(panelCentral);
				panelCentral = new EmitirLicencia();
				contentPane.add(panelCentral,BorderLayout.CENTER);
				setSize(new Dimension(601,501));
			}
		});
		
		botonImprimirLicencia.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				contentPane.remove(panelCentral);
				panelCentral = new ImprimirLicencia();
				contentPane.add(panelCentral,BorderLayout.CENTER);
				setSize(new Dimension(602,500));
			}
		});
	}
	
	public void verificarUsuario(){
		if(admUsuario.getUsuarioActual().getRol().getId()==2)
			botonAltaUsuario.setEnabled(true);
		else
			botonAltaUsuario.setEnabled(false);
	}
	
	public void limpiar(){
		contentPane.remove(panelCentral);
		panelCentral = new JPanel();
		panelCentral.setBorder(new TitledBorder(new LineBorder(new Color(184, 207, 229)), "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		contentPane.add(panelCentral,BorderLayout.CENTER);
		setSize(new Dimension(603,503));
	}
	
}
